package com.atmilan.quartz.config;

import cn.hutool.core.util.StrUtil;
import com.atmilan.quartz.model.dto.delloglio.DelloglioProductResDTO;
import com.atmilan.quartz.model.dto.delloglio.DelloglioProductResDTO.DelloglioGoodsRes;
import com.atmilan.quartz.model.dto.delloglio.DelloglioProductResDTO.DelloglioGoodsRes.Item;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class DelloglioXmlSaxHandler extends DefaultHandler {

    DelloglioProductResDTO delloglioProductResDTO;
    List<DelloglioGoodsRes> goodList = new ArrayList<>();
    DelloglioGoodsRes delloglioGoodsRes;
    List<Item> itemList;
    Item item;
    String tag = "";
    boolean flag = true;

    @Override
    public void startDocument() {
        log.info("---------开始解析Delloglio商品xml--------------");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) {
        if("products".equals(qName)){
            delloglioProductResDTO = new DelloglioProductResDTO();
            if(attributes!=null){
                 for(int i=0;i<attributes.getLength();i++){
                     if("number".equals(attributes.getQName(i))){
                         delloglioProductResDTO.setNumber(Integer.parseInt(attributes.getValue(i)));
                     }
                 }
             }
        }
        if("product".equals(qName)){
            delloglioGoodsRes = new DelloglioGoodsRes();
            flag = true;
        }
        if("items".equals(qName)){
            itemList = new ArrayList<>();
            flag = false;
        }
        if("item".equals(qName)){
            item = new Item();
        }
        tag = qName;
    }

    @Override
    public void characters(char[] ch, int start, int length) {
        String value = new String(ch, start, length).trim();
        if(!value.isEmpty() && StringUtils.isNotBlank(tag)){
            if(flag){
                setField(value, delloglioGoodsRes);
            }else {
                setField(value, item);
            }
        }
    }

    private <T> void setField(String value,T t) {
        Class<?> clazz = t.getClass();
        Field[] fields = clazz.getDeclaredFields();
        String fieldName = StrUtil.toCamelCase(tag);
        for (Field field : fields) {
            if (fieldName.equals(field.getName())){
                String name = field.getName();
                char[] chars = name.toCharArray();
                chars[0] -= 32;
                String tMethodName = "set" + String.valueOf(chars);
                Method method = null;
                try {
                    method = clazz.getMethod(tMethodName, field.getType());
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
                try {
                    method.invoke(t,value);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) {
        if("item".equals(qName)){
            itemList.add(item);
        }
        if("items".equals(qName)){
            delloglioGoodsRes.setItems(itemList);
        }
        if("product".equals(qName)){
            goodList.add(delloglioGoodsRes);
        }
        if("products".equals(qName)){
            delloglioProductResDTO.setProducts(goodList);
        }
        tag = "";
    }

    @Override
    public void endDocument() {
        log.info("---------Delloglio商品xml解析完成--------------");
    }

    public DelloglioProductResDTO getDelloglioProductResDTO(){
        return delloglioProductResDTO;
    }

}
